package eerr.llrr.oorr.pte9983.b.pl190.host668.notification;

import android.annotation.SuppressLint
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.media.AudioAttributes
import android.os.Build
import android.util.Log
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import cccf.jjjf.bbbf.R
import com.manager.core.pl190.host668.TurenUtils
import java.util.UUID

/**
 * Created by ZhangDeCheng on 2023/6/27
 */
private const val TAG = "Notification_A"

class NotificationAlert {
    private val mCustomChannelId = "notifyId"
    private val mCustomChannelName = "notify"
    private var mFlag = 0
    private val mCustomNotificationId = 9006 // 通知id

    private lateinit var mBuilder: NotificationCompat.Builder

    fun cancelNotification(context: Context) {
        try {
            Log.e(TAG, "cancelNotification: ")
            val mManager = NotificationManagerCompat.from(context)
            mManager.cancel(mCustomNotificationId)
        } catch (e: Exception) {
            Log.e(TAG, "cancelNotification: error:${e.message}")
        }
    }

    @SuppressLint("RemoteViewLayout")
    fun showNotification(context: Context, index: Int) {
        try {
            val mManager = NotificationManagerCompat.from(context)
            if (!mManager.areNotificationsEnabled()) {
                Log.e(TAG, "showNotification: switch off")
                return
            }

            // 适配8.0及以上
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val channel = NotificationChannel(
                    mCustomChannelId,
                    mCustomChannelName,
                    NotificationManager.IMPORTANCE_HIGH
                )
                channel.vibrationPattern = null
                channel.setSound(
                    null,
                    AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build()
                )
                channel.enableVibration(false)

                mManager.createNotificationChannel(channel)

                val channel2: NotificationChannel? =
                    mManager.getNotificationChannel(mCustomChannelId)
                if (channel2 != null && channel2.importance == 0) {
                    Log.e(TAG, "showNotification: channel2.importance == 0")
                    return
                }
            }

            // 适配12.0及以上
            mFlag =
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                    PendingIntent.FLAG_IMMUTABLE
                } else {
                    PendingIntent.FLAG_UPDATE_CURRENT
                }

            var layoutId = R.layout.layout_notification_1

            Log.e(TAG, "showNotification: index:$index")
            when (index) {
                0 -> {
                    layoutId = R.layout.layout_notification_1
                }

                1 -> {
                    layoutId = R.layout.layout_notification_2
                }

                2 -> {
                    layoutId = R.layout.layout_notification_3
                }
            }

            // 添加自定义通知view
            val views = RemoteViews(context.packageName, layoutId)

            // TODO 首页 内容点击事件
            val clickIntent = Intent(MyNotificationManager.NOTIFY_CLICK_ACTION)
            clickIntent.setPackage(context.packageName)
            val clickPending =
                PendingIntent.getActivity(context, UUID.randomUUID().hashCode(), clickIntent, mFlag)

            val deleteIntent = Intent(MyNotificationManager.NOTIFY_DELETE_ACTION)
            val deletePending = PendingIntent.getBroadcast(
                context,
                UUID.randomUUID().hashCode(),
                deleteIntent,
                mFlag
            )

            views.setOnClickPendingIntent(R.id.btn_opt, clickPending)

            // 创建Builder
            mBuilder = NotificationCompat.Builder(context, mCustomChannelId)
                .setSmallIcon(R.mipmap.ic_notification)
                .setLargeIcon(
                    BitmapFactory.decodeResource(
                        context.resources,
                        R.mipmap.ic_notification
                    )
                )
                .setAutoCancel(true)
                .setContent(views)
                .setCustomContentView(views)


            val builder = mBuilder

//            builder.setFullScreenIntent(
//                PendingIntent.getBroadcast(
//                    context,
//                    0,
//                    Intent("Action.Scene.Fake"),
//                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_IMMUTABLE else 0
//                ), true
//            )

//            builder.setFullScreenIntent(deletePending,true)

            builder.setOngoing(false)
            builder.setContentTitle("notify")
            builder.setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE)
            builder.setVibrate(null)

            builder.setContentIntent(clickPending)
            builder.setDeleteIntent(deletePending)

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
                builder.setCustomBigContentView(views)
            }

//            builder.priority = NotificationManager.IMPORTANCE_HIGH // TODO
//            builder.setPriority(1000)
            builder.setOnlyAlertOnce(false)
            builder.setCustomHeadsUpContentView(views)
//            builder.setCategory(Notification.CATEGORY_SYSTEM) // TODO
//            builder.setVisibility(Notification.VISIBILITY_PUBLIC)// TODO

            // 发起通知
            mManager.notify(mCustomNotificationId, mBuilder.build())
            TurenUtils.getInstance().updateAdShow()
        } catch (e: Exception) {
            Log.e(TAG, "showNotification: error:${e.message}")
        }
    }
}